/**
 *  @file: PageManager.cpp
 *  @author: nwz
 *  @brief: 
 *  @date: 2024年8月6日
 * 
 */
#define TAG "page_manager"
#include <all_header.h>
#include <code/bsp/PageManager.h>
#include <code/ui/ui.h>
#include <code/ui/screen/ui_HomePage.h>
#include <code/ui/screen/ui_AboutPage.h>
#include <code/ui/screen/ui_SetPage.h>
#include <code/ui/screen/ui_MenuPage.h>
#include <code/ui/screen/ui_ChargPage.h>
#include <code/ui/screen/ui_CompassPage.h>
#include <code/ui/screen/ui_DateTimeSetPage.h>
#include <code/ui/screen/ui_EnvPage.h>
#include <code/ui/screen/ui_HRPage.h>
#include <code/ui/screen/ui_OffTimePage.h>
#include <code/ui/screen/ui_SetPage.h>
#include <code/ui/screen/ui_TimerPage.h>

namespace task {

typedef struct {
	PageId id;
	void (*init)(void);
	void (*deinit)(void);
	lv_obj_t **obj;
} page_data_t;

#define PAGE(name)  {PageId::name, Page_##name.init, Page_##name.deinit, Page_##name.page_obj}

static page_data_t pages[] = {
//
		{ PageId::Empty, nullptr, nullptr, nullptr },
//		//
		PAGE(Home),
		//
		PAGE(About),
		//
		PAGE(Set),
		//
		PAGE(Menu),
		//
		PAGE(Power),
		//
		PAGE(Charg),
		//
		PAGE(Compass),
		//
		PAGE(DateTimeSet),
		//
		PAGE(Env),
		//
		PAGE(HR),
		//
		PAGE(LOffTimeSet),
		//
		PAGE(TOffTimeSet),
		//
		PAGE(Timer),
		//
		};

PageManager::PageManager() {
	// TODO Auto-generated constructor stub
	for (int i = 0; i < ARRAY_SIZE(pages); i++) {
		ASSERT(i == (int )pages[i].id);
	}
}

PageManager::~PageManager() {
	// TODO Auto-generated destructor stub
}

void PageManager::init() {
	loadDetail(PageId::Home, LV_SCR_LOAD_ANIM_NONE, 0);
}

void PageManager::notifyPageChange() {
	MainTask::notifyUiPageChange();
}

void PageManager::push(PageId id) {
	stack[page_top++] = id;
	ASSERT(page_top < MAX_SIZE);
}

void PageManager::load(PageId id) {
	auto page = pages[(int) id];
	push(id);
	page.init();
	lv_scr_load_anim(*page.obj, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 100, 0, true);
	notifyPageChange();
}

void PageManager::loadDetail(PageId id, lv_scr_load_anim_t anim,
		uint32_t time) {
	auto page = pages[(int) id];
	push(id);
	page.init();
	lv_scr_load_anim(*page.obj, anim, time, 0, true);
	notifyPageChange();
}

void PageManager::popBack() {
	if (page_top < 2) {
		// homepage
		if (getCurrenPage() != PageId::Home) {
			auto page = pages[(int) PageId::Home];
			page.init();
			lv_scr_load_anim(*page.obj, LV_SCR_LOAD_ANIM_MOVE_LEFT, 100, 0,
					true);
			notifyPageChange();
		}
	} else {
		// pop
		pop();
		auto page = pages[(int) stack[page_top - 1]];
		page.init();
		lv_scr_load_anim(*page.obj, LV_SCR_LOAD_ANIM_MOVE_LEFT, 100, 0, true);
		notifyPageChange();
	}
}

PageId PageManager::pop() {
	return stack[--page_top];
}

PageId PageManager::getCurrenPage() {
	if (page_top == 0)
		return PageId::Empty;
	return stack[page_top - 1];
}
void PageManager::onKeyEvent(void *event) {
	switch (((UserKeyTask::notify_event_t*) event)->id) {
	case UserKeyTask::Key::K1:
		if (page_top < ARRAY_SIZE(pages) - 1) {
			load(pages[page_top + 1].id);
		}
		break;
	case UserKeyTask::Key::K2:
		popBack();
		break;
	}
}

} /* namespace task */
